package com.jichangxiu.common.utils;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HTMLFilter;

import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrUtils extends StrUtil {

    private static final Pattern NUMBER_PATTERN = Pattern.compile("-?[0-9]+(\\[0-9]+)?");

    private static final ThreadLocal<HTMLFilter> HTML_FILTER = ThreadLocal.withInitial(() -> {
        HTMLFilter htmlFilter = new HTMLFilter();
        // 避免 " 被转成 &quot; 字符
        ReflectUtil.setFieldValue(htmlFilter, "encodeQuotes", false);
        return htmlFilter;
    });

    public static String wordFirstBig(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    public static String wordFirstSmall(String str) {
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static String wordEachKebabCase(String str) {
        return str.replaceAll("_", "-");
    }

    public static String wordEachBigFs(String str) {
        return wordFirstSmall(wordEachBig(str));
    }

    public static String wordHumpToLine(String str) {
        return str.replaceAll("[A-Z]", "_$0").toLowerCase();
    }

    public static String wordEachBig(String str) {
        StringBuilder newStr = new StringBuilder();
        for (String s : str.split("_"))
            newStr.append(wordFirstBig(s));
        return newStr.toString();
    }

    public static boolean checkNameChinese(String name) {
        boolean res = true;
        char[] cTemp = name.toCharArray();
        for (int i = 0; i < name.length(); i++)
            if (!isRightChar(cTemp[i])) {
                res = false;
                break;
            }
        return res;
    }

    public static boolean isRightChar(char c) {
        return isChinese(c) || isWord(c);
    }

    public static boolean isWord(char c) {
        String regEx = "[\\w]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(String.valueOf(c));
        return m.matches();
    }

    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
    }

    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();
    }

    public static boolean isChineseChar(char c) {
        try {
            return String.valueOf(c).getBytes(StandardCharsets.UTF_8).length > 1;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean checkNameIsWord(String name) {
        boolean res = true;
        char[] cTemp = name.toCharArray();
        for (int i = 0; i < name.length(); i++)
            if (!isWord(cTemp[i])) {
                res = false;
                break;
            }
        return res;
    }

    public static int checkIsChinese(String str) {
        String regex3 = "[\\u4e00-\\u9fa5]+";
        if (!str.matches(regex3))
            return 1;
        return 2;
    }

    public static int isThereNumber(String str) {
        String regex2 = ".*[0-9].*";
        if (!str.matches(regex2))
            return 3;
        return 4;
    }

    public static boolean checkStrIsNum02(String str) {
        Matcher isNum = NUMBER_PATTERN.matcher(str);
        return isNum.matches();
    }

    public static String convertToCamelCase(String name) {
        StringBuilder result = new StringBuilder();
        // 快速检查
        if (name == null || name.isEmpty())
            // 没必要转换
            return "";
        else if (!name.contains("_"))
            // 不含下划线，仅将首字母大写
            return name.substring(0, 1).toUpperCase() + name.substring(1);
        // 用下划线将原始字符串分割
        String[] camels = name.split("_");
        for (String camel : camels) {
            // 跳过原始字符串中开头、结尾的下换线或双重下划线
            if (camel.isEmpty())
                continue;
            // 首字母大写
            result.append(camel.substring(0, 1).toUpperCase());
            result.append(camel.substring(1).toLowerCase());
        }
        return result.toString();
    }

    public static String format(String template, Object... params) {
        if (ObjectUtil.isEmpty(params) || StrUtil.isEmpty(template))
            return template;
        return StrFormatter.format(template, params);
    }

    public static String xssFilter(String content) {
        return HTML_FILTER.get().filter(content);
    }

}
